/*
 * This file is part of the Try! Engine project.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#ifndef TRY_SCENETRANSFORMABLE_H
#define TRY_SCENETRANSFORMABLE_H

#include "scenenode.h"
#include <property.h>

namespace Try
{
    class SceneTransformable : public Property, public SceneNodeListener
    {
    public:
        struct Msg
        {
            static MessageName sceneNodeChanged;
            static MessageName parentNodeChanged;
            static MessageName transformChanged;
        };

    protected:
        SceneNode* m_sceneNode;

    public:
        virtual ~SceneTransformable();

        void setSceneNode(SceneNode* node);
        void setParentNode(SceneNode* node);

        void resetTransform();
        void setTransform(const Transform& t);
        void setPosition(const Vector3& pos);
        void setRotation(const Quaternion& q);
        void setRotation(const Vector3& rot);
        void setScale(const Vector3& scale);

        void translate(const Vector3& dp);
        void rotate(const Quaternion& dr);
        void rotate(const Vector3& dr);
        void resize(const Vector3& ds);

        void lookAt(const Vector3& target);

        template<class T>
        T* sceneNode() const;

        SceneNode* sceneNode() const;
        SceneNode* parentNode() const;

        Transform transform() const;
        AxisAlignedBoundingBox worldAABB() const;
        Vector3 position() const;
        Quaternion rotation() const;
        Vector3 scale() const;

        void onTransformChanged(SceneNode* n, const Transform& t);
        void onParentChanged(SceneNode* n, SceneNode* p);
        void onDeleting(SceneNode* n);

        static std::vector<SceneTransformable*> query(SceneNode* node);

    protected:
        SceneTransformable(const String& name, Object* owner, SceneNode* node);
    };

    template<class T>
    T* SceneTransformable::sceneNode() const
    {
        return static_cast<T*>(m_sceneNode);
    }
}

#endif // TRY_SCENETRANSFORMABLE_H
